package com.bosicloud.cmb.ibp.api.manager.util;

import com.alibaba.fastjson.JSONObject;
import com.bosicloud.cmb.ibp.api.manager.commom.vo.ResponseHeaderVO;
import com.bosicloud.cmb.ibp.api.manager.commom.vo.ResponseMessagesVO;
import io.swagger.models.Model;
import io.swagger.models.Response;
import io.swagger.models.Swagger;
import io.swagger.models.properties.Property;
import io.swagger.models.properties.RefProperty;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Slf4j
public class ResponseProcessor {

    private final Swagger swagger;
    private final ModelProcessor modelProcessor;

    public ResponseProcessor(Swagger swagger) {
        this.swagger = swagger;
        modelProcessor = new ModelProcessor(swagger);

    }

    public List<ResponseMessagesVO> processor(Map<String, Response> responseMap) {

        List<ResponseMessagesVO> responseMessagesVOList = new ArrayList<>();
        if (MapUtils.isNotEmpty(responseMap)) {
            for (Map.Entry<String, Response> entry : responseMap.entrySet()) {
                ResponseMessagesVO.ResponseMessagesVOBuilder builder = ResponseMessagesVO.builder();
                final Response response = entry.getValue();
                builder.httpStatusCode(entry.getKey());
                builder.reason(StringUtils.defaultIfEmpty(response.getDescription(), null));
                builder.responseModel(processRefProperty(response).toJSONString());
                builder.responseHeaderVOS(processHeaders(response));
                responseMessagesVOList.add(builder.build());
            }
        }
        return responseMessagesVOList;
    }

    private List<ResponseHeaderVO> processHeaders(Response response) {
        Map<String, Property> responseHeaders = response.getHeaders();
        List<ResponseHeaderVO> headerList = new ArrayList<>();
        if (MapUtils.isNotEmpty(responseHeaders)) {
            for (Map.Entry<String, Property> headerEntry : responseHeaders.entrySet()) {
                Property property = headerEntry.getValue();
                ResponseHeaderVO responseHeaderVO = ResponseHeaderVO.builder()
                        .name(headerEntry.getKey())
                        .desp(StringUtils.defaultIfEmpty(property.getDescription(), null))
                        .paramType(StringUtils.defaultIfEmpty(property.getType(), null))
                        .build();

                headerList.add(responseHeaderVO);
            }
        }
        return headerList;
    }

    private JSONObject processRefProperty(Response response) {
        JSONObject jsonObject = new JSONObject();
        Property schema = response.getSchema();
        if (schema instanceof RefProperty) {
            RefProperty refProperty = (RefProperty) schema;
            Map<String, Model> modelMap = swagger.getDefinitions();
            Model model = modelMap.get(refProperty.getSimpleRef());
            if (Objects.nonNull(model)) {
                jsonObject = modelProcessor.processModel(model);
                log.info("Response Model:{}", jsonObject.toJSONString());
            }
        }
        return jsonObject;
    }

}
